package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.util;
/**
 * Created by Administrator on 2017/3/13.
 */


import com.kingyun.gpsinspection.purificationservice.common.enums.TreeIconSkinEnum;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Ztree;

import java.util.Iterator;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 组织工具类
 * @CreateDateTime 2017-03-13 19:39
 **/
public class OrgTreeUtil {

    /**
     * 处理org转tree
     *
     * @param orglist  当前信息
     * @param itemList 子项信息
     * @param demotree tree集合
     */
    public static void orgToTree(List<Organization> orglist, List<Organization> itemList, List<Ztree> demotree) {
        if (orglist != null && orglist.size() > 0) {
            for (Organization org : orglist) {
                Ztree demo = new Ztree();
                demo.setId(org.getOrgId());
                demo.setpId(org.getParentOrgId());
                demo.setName(org.getOrgName());
                demo.setOrgLevel(org.getOrgLevelId());
                int levelId = Integer.parseInt(org.getOrgLevelId());
                demo.setIconSkin(iconSkin(levelId));
                //判断是否含有下属机构，含有则为true，没有则为false
                Boolean isParent = isOrg(org.getOrgId(), itemList);
                demo.setIsParent(isParent);
                demo.setOpen(false);
                demotree.add(demo);
            }
        }
    }

    /**
     * 处理org转tree
     *
     * @param orglist  当前信息
     * @param itemList 子项信息
     * @param demotree tree集合
     */
    public static void orgToTree(List<Organization> orglist, List<Organization> itemList, List<Ztree> demotree, String orgId) {
        if (orglist != null && orglist.size() > 0) {
            for (Organization org : orglist) {
                Ztree demo = new Ztree();
                demo.setId(org.getOrgId());
                demo.setpId(org.getParentOrgId());
                demo.setName(org.getOrgName());
                demo.setOrgLevel(org.getOrgLevelId());
                int levelId = Integer.parseInt(org.getOrgLevelId());
                demo.setIconSkin(iconSkin(levelId));
                //判断是否含有下属机构，含有则为true，没有则为false
                Boolean isParent = isOrg(org.getOrgId(), itemList);
                demo.setIsParent(isParent);
                if (org.getOrgId().equals(orgId)) {
                    demo.setOpen(true);
                } else {
                    demo.setOpen(false);
                }
                demotree.add(demo);
            }
        }
    }

    /**
     * 处理org转tree
     *
     * @param orglist  当前信息
     * @param demotree tree集合
     */
    public static void orgToTreeTrue(List<Organization> orglist, List<Ztree> demotree, String orgId) {
        if (orglist != null && orglist.size() > 0) {
            for (Organization org : orglist) {
                Ztree demo = new Ztree();
                demo.setId(org.getOrgId());
                demo.setpId(org.getParentOrgId());
                demo.setName(org.getOrgName());
                demo.setOrgLevel(org.getOrgLevelId());
                int levelId = Integer.parseInt(org.getOrgLevelId());
                demo.setIconSkin(iconSkin(levelId));
                //判断是否含有下属机构，含有则为true，没有则为false
                demo.setOpen(true);
                demotree.add(demo);
            }
        }
    }

    /**
     * 处理org转tree
     *
     * @param orglist  当前信息
     * @param itemList 子项信息
     * @param demotree tree集合
     */
    public static void orgToTreeTrue(List<Organization> orglist, List<Organization> itemList, List<String> list, List<Ztree> demotree, String orgId) {
        if (orglist != null && orglist.size() > 0) {
            for (Organization org : orglist) {
                Ztree demo = new Ztree();
                demo.setId(org.getOrgId());
                demo.setpId(org.getParentOrgId());
                demo.setName(org.getOrgName());
                demo.setOrgLevel(org.getOrgLevelId());
                int levelId = Integer.parseInt(org.getOrgLevelId());
                demo.setIconSkin(iconSkin(levelId));
                //判断是否含有下属机构，含有则为true，没有则为false
                Boolean isParent = isOrg(org.getOrgId(), itemList, list);
                demo.setIsParent(isParent);
                if (org.getOrgId().equals(orgId)) {
                    demo.setOpen(true);
                } else {
                    demo.setOpen(false);
                }
                demotree.add(demo);
            }
        }
    }

    /**
     * 根据层级数获取对应的显示图片
     *
     * @param levelId
     * @return
     */
    public static String iconSkin(int levelId) {
        if (levelId == 1) {
            return TreeIconSkinEnum.ICON_BUIDING.getDesc();
        } else if (levelId == 2) {
            return TreeIconSkinEnum.ICON_FLAG.getDesc();
        } else if (levelId == 3) {
            return TreeIconSkinEnum.ICON_OA.getDesc();
        } else if (levelId == 4) {
            return TreeIconSkinEnum.ICON_STATION.getDesc();
        } else if (levelId == 5) {
            return TreeIconSkinEnum.IONC_WELL.getDesc();
        } else if (levelId == 6) {
            return TreeIconSkinEnum.IONC_PIPELINE.getDesc();
        }
        return null;
    }

    /**
     * 判断当前数据下面是否有子项信息
     *
     * @param orgId
     * @param itemList
     * @return 有就返回true，没有返回false
     */
    private static boolean isOrg(String orgId, List<Organization> itemList) {
        if (itemList != null) {
            Iterator<Organization> it = itemList.iterator();
            while (it.hasNext()) {
                Organization param = it.next();
                if (param.getParentOrgId().equals(orgId)) {
                    it.remove();
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 判断当前数据下面是否有子项信息
     *
     * @param orgId
     * @param itemList
     * @return 有就返回true，没有返回false
     */
    private static boolean isOrg(String orgId, List<Organization> itemList, List<String> list) {
        if (list != null && list.size() > 0) {
            Iterator<String> ite = list.iterator();
            while (ite.hasNext()) {
                String str = ite.next();
                if (str.equals(orgId)) {
                    ite.remove();
                    return true;
                }
            }
        }
        return isOrg(orgId, itemList);
    }
}
